<template>
  <div>
    学生 <br>
    email: 123456@qq.com <br>
    pass: 123456 <br>
    咨询师 <br>
    email: 0757@qq.com <br>
    pass: 123456 <br>
  </div>
  <el-container>
    <el-main>
      <el-row>
        <el-col :xs="0" :md="24">
          <el-card class="box-card">
            <template #header>
              <div class="card-header">
                <span>登录</span>
              </div>
            </template>
            <div>
              <el-form :model="loginUser" status-icon :rules="rules" ref="loginForm" label-width="60px">

                <el-form-item label="邮箱" prop="email">
                  <el-input v-model="loginUser.email" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="pass">
                  <el-input type="password" v-model="loginUser.pass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="submitForm('loginForm')" style="float: left">登录</el-button>
                  <el-button @click="resetForm('loginForm')" style="float: right">重置</el-button>
                </el-form-item>
              </el-form>
              <div class="card-footer">
                <router-link type="primary" style="float: left" to="/reg">注册账户？</router-link>
                <router-link type="danger" style="float: right" to="/forget">忘记密码？</router-link>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>


      <el-row>
        <el-col :xs="24" :sm="0">
          <el-card>
            <template #header>
              <div>
                <span>登录</span>
              </div>
            </template>
            <div>
              <el-form :model="loginUser" status-icon :rules="rules" ref="loginForm" label-width="60px">

                <el-form-item label="邮箱" prop="email">
                  <el-input v-model="loginUser.email" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="pass">
                  <el-input type="password" v-model="loginUser.pass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="submitForm('loginForm')" style="float: left">登录</el-button>
                  <el-button @click="resetForm('loginForm')" style="float: right">重置</el-button>
                </el-form-item>
              </el-form>
              <div class="card-footer">
                <el-link type="primary" style="float: left" @click="toPage('/reg')">注册账户？</el-link>
                <el-link type="danger" style="float: right" @click="toPage('/forget')">忘记密码？</el-link>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-main>
  </el-container>
</template>

<script>
import {login} from "@/api/user";
import router from "@/router";
import {saveUser} from "@/utils/cookies";

export default {
  name: "login",
  data() {
    return {
      loginUser: {
        email: '123456@qq.com',
        pass: '123456',
      },
      rules: {
        email: [
          {required: true, message: '邮箱不能为空', trigger: 'blur'},
          {min: 5, max: 32, message: '邮箱长度在5-32个字符之间', trigger: 'blur'},
          {type: 'email', message: '邮箱格式错误', trigger: 'blur'},
        ],
        pass: [
          {required: true, message: '密码不能为空', trigger: 'blur'},
          {min: 5, max: 20, message: '密码长度在5-20个字符之间', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    submitForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(this.loginUser).then(res => {
            if (res.status) {
              saveUser(res.data)
              router.push('/')
            }
          })
        }
      })
    },
    resetForm(formName){
      this.$refs[formName].resetFields();
    },
    toPage(path){
      router.push(path)
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  width: 100%;
  height: 100%;
  background-color: #2c3e50;

  .el-main {
    height: 500px;
    margin: auto;

    .box-card {
      width: 400px;
      margin: auto;

      .card-header {
        text-align: left;
      }
    }
  }
}
</style>
